﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.IO;
namespace BombermanGameEditorBeta
{
    //--------Read/Write File---------//
    class FileManager
    {
        //public void writeLandscape() { }
        //public void writeHero() { }
        //public void writeEnemy() { }
        //public void writeWall() { }
        //public void writeBox() { }



        /// <summary>
        /// load
        /// </summary>
        public void loadFile(MapBox[,] boxs, int row, int column) 
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.FileName = "地图";
            ofd.DefaultExt = "*.*";
            ofd.Filter = "地图文件(.map)|*.map|文本文件(.txt)|*.txt";

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                StreamReader sr = new StreamReader(ofd.FileName);
                //filePath.Text = "图片路径：" + dlg.FileName; sort.IsEnabled = true;

                String line;
                while ((line = sr.ReadLine()) != null)
                {
                    
                    //split the line to 3 strings
                    string[] s = line.Split(' ');

                    int r=int.Parse(s[0]);
                    int c=int.Parse(s[1]);
                    string type=s[2];

                    //turn string to enum
                    EnumTypes tempType;
                    tempType=(EnumTypes)Enum.Parse(typeof(EnumTypes), type);
                    boxs[r, c].type = tempType;

                }

                sr.Close();
            }
        }

        /// <summary>
        /// save
        /// </summary>
        /// <param name="name"></param>
        public void saveFile(MapBox[,] boxs, int row, int column) 
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.FileName = "地图";
            sfd.DefaultExt = "*.*";
            sfd.Filter = "地图文件(.map)|*.map|文本文件(.txt)|*.txt";

            if (sfd.ShowDialog() == DialogResult.OK)
            {

                StreamWriter sw = new StreamWriter(sfd.FileName,false);

                for (int ro = 0; ro < row; ro++)
                {
                    for (int col = 0; col < column; col++)
                    {
                        string temp = ro +" "+ col + " "+ boxs[ro, col].type.ToString();
                        sw.WriteLine(temp);
                    }
                }
                sw.Close();
            }

        }
    }
}
